package controllers

import (
	"strconv"

	"github.com/gin-gonic/gin"
	doctor_dao "jjqzplucky.site/sz-core/internal/dao/doctor"
	"jjqzplucky.site/sz-core/internal/models"
	doctor_model "jjqzplucky.site/sz-core/internal/models/doctor"
	"jjqzplucky.site/sz-core/pkg/constants"
	"jjqzplucky.site/sz-core/pkg/utils"
)

type DoctorController struct {
}

func (d *DoctorController) GetDoctorByID(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.ParseUint(id, 10, 64)
	doctor := doctor_model.Doctor{
		BaseModel: models.BaseModel{Id: uid},
	}

	if err := doctor_dao.GetDoctorByID(&doctor); err != nil {
		_ = c.Error(err)
		return
	}
	c.Set(constants.ResultData, doctor)
}

//医生分页
func (d *DoctorController) GetDoctors(c *gin.Context) {
	var params doctor_model.QueryParams
	err := c.ShouldBindQuery(&params)
	if err != nil {
		_ = c.Error(err)
		return
	}
	if list, err := doctor_dao.Page(&params); err != nil {
		_ = c.Error(err)
		return
	} else {
		c.Set(constants.ResultData, utils.PageResult{
			Page: params.Page,
			PageSize: params.PageSize,
			Total: params.Total,
			List: list,
		})
	}
}


//提交医生信息
func (d *DoctorController) CommitDoctor(c *gin.Context) {
	var doctorDto doctor_model.DoctorDto
	var doctor doctor_model.Doctor
	err := c.ShouldBind(&doctorDto)
	utils.CopyStruct(&doctorDto, &doctor)
	if err != nil {
		_ = c.Error(err)
		return
	}
	if err := doctor_dao.SaveDoctor(&doctor); err != nil {
		_ = c.Error(err)
		return
	}
	c.Set(constants.ResultData, doctor)
}

//删除医生
func (d *DoctorController) DeleteDoctor(c *gin.Context) {
	id := c.Query("id")
	uid, _ := strconv.ParseUint(id, 10, 64)
	if err := doctor_dao.DeleteById(uid); err != nil {
		_ = c.Error(err)
		return
	}
	c.Set(constants.ResultData, nil)
}